<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 巡检用户检查 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-circle-plus" @click="handleAdd">新增</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="index" label="序号" width="55" align="center"></el-table-column>
                <el-table-column prop="updateBy" label="姓名"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.thumb" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="负责的停车点"></el-table-column>
                <el-table-column prop="parkingLotIds" label="打卡的点位"></el-table-column>

                <el-table-column label="操作" width="250" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                        <el-button type="text" icon="el-icon-edit" @click="handleReset(scope.$index, scope.row)">重置密码</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="dialogTtile" :visible.sync="editVisible" width="50%" @close="handleDialogClose">
            <el-form ref="form" :model="form" :rules="rules" label-width="90px">
                <div style="display: flex; flex-wrap: wrap; justify-content: flex-start">
                    <div style="flex: 1 1 35%; display: flex; flex-wrap: wrap">
                        <el-form-item label="人员姓名" prop="name">
                            <el-input placeholder="请输入人员姓名" v-model="form.updateBy"></el-input>
                        </el-form-item>
                        <el-form-item label="电话号码" prop="address">
                            <el-input placeholder="请输入电话号码" v-model="form.address"></el-input>
                        </el-form-item>
                        <el-form-item label="初始密码" prop="email">
                            <el-input placeholder="请输入初始密码" v-model="form.email"></el-input>
                        </el-form-item>
                    </div>
                    <el-form-item label="" style="flex: 1 2 50%; align-self: flex-start">
                        <el-upload
                            class="avatar-uploader"
                            action="https://jsonplaceholder.typicode.com/posts/"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                        >
                            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            <div slot="tip" class="el-upload__tip">只支持jpg,png格式</div>
                        </el-upload>
                    </el-form-item>
                </div>
                <div style="display: flex; flex-wrap: wrap">
                    <el-form-item label="停车点" style="flex: 1 1 50%" prop="value1">
                        <el-select
                            v-model="value1"
                            multiple
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请选择或者输入停车点"
                        >
                            <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="签到点" style="flex: 1 1 50%" prop="value2">
                        <el-select
                            v-model="value2"
                            multiple
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请选择或者输入签到点"
                        >
                            <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                        </el-select>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { InspectionUser } from '../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            value: null,
            value1: '',
            options1: [
                { value: '选项1', label: '停车点1' },
                { value: '选项2', label: '停车点2' },
                { value: '选项3', label: '停车点3' }
            ],
            value2: '',
            options2: [
                { value: '选项1', label: '签到点1' },
                { value: '选项2', label: '签到点2' },
                { value: '选项3', label: '签到点3' }
            ],
            imageUrl:'',
            rules: {
                name: [{ required: true, message: '请输入人员姓名', trigger: 'blur' }],
                address: [{ required: true, message: '请输入电话号码', trigger: 'blur' }],
                email: [{ required: true, message: '请输入初始密码', trigger: 'blur' }],
                value1: [{ required: true, message: '请选择或者输入停车点', trigger: 'change' }],
                value2: [{ required: true, message: '请选择或者输入签到点', trigger: 'change' }]
            },
            fileList: [],
            dialogTtile: '暂无',
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.fetchData();
    },
    methods: {
        handleAvatarSuccess(){

        },
        beforeAvatarUpload(){},
        handlePreview(file) {
            console.log('::file: ', file);
        },
        handleRemove(file, fileList) {
            console.log('::file: ', file);
            console.log('::fileList: ', fileList);
        },
        // 获取 easy-mock 的模拟数据
        fetchData() {
            InspectionUser(this.query).then((res) => {
                console.log(res,'获取到的数据');
                this.tableData = res.result;
                this.pageTotal = res.pageTotal || 50;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.fetchData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.tableData.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = { ...row };
            this.editVisible = true;
        },
        // 新增
        handleAdd() {
            this.idx = '';
            this.form = {};
            this.editVisible = true;
        },
        // 重置密码
        handleReset() {
            this.$confirm('确定要重置密码吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('重置成功');
                })
                .catch(() => {});
        },
        // 保存编辑
        saveEdit() {
            this.$refs.form.validate((valid) => {
                console.log('::valid: ', valid);
                if (valid) {
                    this.editVisible = false;
                    this.$message.success('保存成功');
                    // 保存逻辑
                } else {
                    this.$message.error('请填写完整信息');
                    return false;
                }
            });
        },
        // 关闭弹窗时重置表单
        handleDialogClose() {
            this.$refs.form.resetFields();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.fetchData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    display: flex;
    justify-content: flex-end;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
